package com.xbongbong.pro.statistics.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.help.CommonHelp;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.statistic.pojo.dto.CategoryFieldGetDTO;
import com.xbongbong.pro.statistic.pojo.dto.CategoryPermissionSaveDTO;
import com.xbongbong.pro.statistic.pojo.dto.CategoryPublishDTO;
import com.xbongbong.pro.statistic.pojo.dto.CategorySearchGetDTO;
import com.xbongbong.pro.statistic.pojo.dto.CategorySearchGetListDTO;
import com.xbongbong.pro.statistic.pojo.dto.CategorySearchSaveBatchDTO;
import com.xbongbong.pro.statistic.pojo.dto.CategorySearchSaveDTO;
import com.xbongbong.pro.statistic.pojo.dto.CategoryTextGetDTO;
import com.xbongbong.pro.statistic.pojo.dto.CategoryTextGetListDTO;
import com.xbongbong.pro.statistic.pojo.dto.CategoryTextSaveDTO;
import com.xbongbong.pro.statistic.pojo.dto.CategoryUpgradeDTO;
import com.xbongbong.pro.statistic.pojo.dto.ChartCategoryDeleteDTO;
import com.xbongbong.pro.statistic.pojo.dto.ChartCategoryEnableDTO;
import com.xbongbong.pro.statistic.pojo.dto.ChartCategoryFormDTO;
import com.xbongbong.pro.statistic.pojo.dto.ChartCategoryGetDTO;
import com.xbongbong.pro.statistic.pojo.dto.ChartCategoryInitDTO;
import com.xbongbong.pro.statistic.pojo.dto.ChartCategoryListDTO;
import com.xbongbong.pro.statistic.pojo.dto.ChartCategorySaveDTO;
import com.xbongbong.pro.statistic.pojo.dto.ChartCategorySortDTO;
import com.xbongbong.pro.statistic.pojo.dto.ChartMobileCategoryListDTO;
import com.xbongbong.pro.statistic.pojo.vo.CategoryFieldGetVO;
import com.xbongbong.pro.statistic.pojo.vo.CategorySearchGetListVO;
import com.xbongbong.pro.statistic.pojo.vo.CategorySearchGetVO;
import com.xbongbong.pro.statistic.pojo.vo.CategorySearchVO;
import com.xbongbong.pro.statistic.pojo.vo.CategoryTextGetListVO;
import com.xbongbong.pro.statistic.pojo.vo.CategoryTextGetVO;
import com.xbongbong.pro.statistic.pojo.vo.CategoryTextVO;
import com.xbongbong.pro.statistic.pojo.vo.ChartCategoryDeleteVO;
import com.xbongbong.pro.statistic.pojo.vo.ChartCategoryEnableVO;
import com.xbongbong.pro.statistic.pojo.vo.ChartCategoryFormVO;
import com.xbongbong.pro.statistic.pojo.vo.ChartCategoryGetVO;
import com.xbongbong.pro.statistic.pojo.vo.ChartCategoryIndexListVO;
import com.xbongbong.pro.statistic.pojo.vo.ChartCategoryInitVO;
import com.xbongbong.pro.statistic.pojo.vo.ChartCategoryListVO;
import com.xbongbong.pro.statistic.pojo.vo.ChartCategorySaveVO;
import com.xbongbong.pro.statistic.pojo.vo.ChartCategorySortVO;
import com.xbongbong.pro.statistic.result.pojo.dto.ChartCategoryListByIdInDTO;
import com.xbongbong.pro.statistic.result.pojo.vo.ChartCategoryListByIdInVO;
import com.xbongbong.pro.statistic.service.ChartCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 报表分类相关
 * @author zcp
 * @version v1.0
 * @date 2019/1/16 9:53
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/chart")
public class ChartCategoryController {
    private static final Logger LOG = LoggerFactory.getLogger(ChartCategoryController.class);
    @Resource
    private ChartCategoryService chartCategoryService;
    @Resource
    private PaasRedisHelper paasRedisHelper;
    @Resource
    private CommonHelp commonHelp;

    /**
     * 首页左侧菜单展示----图表中心二级分组下的图表也要渲染进来
     * @param chartCategoryListDTO 入参
     * @param br 校验信息
     * @return java.lang.String response
     * @author zcp
     * @date 2019/1/16 10:02
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/category/index/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartCategoryIndexList(@RequestBody @Valid ChartCategoryListDTO chartCategoryListDTO, BindingResult br) {
        XbbResponse<ChartCategoryIndexListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ChartCategoryIndexListVO chartCategoryIndexListVO = chartCategoryService.chartCategoryIndexList(chartCategoryListDTO);
                response = new XbbResponse<>(chartCategoryIndexListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
       return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 图表中心左侧菜单展示
     * @param chartCategoryListDTO 入参
     * @param br 校验信息
     * @return java.lang.String response
     * @author zcp
     * @date 2019/1/16 10:27
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/category/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartCategoryList(@RequestBody @Valid ChartCategoryListDTO chartCategoryListDTO, BindingResult br) {
        XbbResponse<ChartCategoryListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ChartCategoryListVO result = chartCategoryService.chartCategoryList(chartCategoryListDTO);
                response = new XbbResponse<>(result);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
       return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 图表中心pk榜特殊模版筛选
     * @param chartCategoryFormDTO 入参
     * @param br 校验信息
     * @return java.lang.String response
     * @author zcp
     * @date 2019/1/16 10:27
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/category/form", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartCategoryForm(@RequestBody @Valid ChartCategoryFormDTO chartCategoryFormDTO, BindingResult br) {
        XbbResponse<ChartCategoryFormVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ChartCategoryFormVO result = chartCategoryService.chartCategoryForm(chartCategoryFormDTO);
                response = new XbbResponse<>(result);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取单个分组信息
     * @param chartCategoryGetDTO 入参
     * @param br 校验信息
     * @return java.lang.String response
     * @author zcp
     * @date 2019/1/16 10:32
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/category/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartCategoryGet(@RequestBody @Valid ChartCategoryGetDTO chartCategoryGetDTO, BindingResult br) {
        XbbResponse<ChartCategoryGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ChartCategoryGetVO chartCategoryGetVO = chartCategoryService.chartCategoryGet(chartCategoryGetDTO);
                response = new XbbResponse<>(chartCategoryGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
       return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 新建/编辑分组
     * @param chartCategoryGetDTO 入参
     * @param br 校验信息
     * @return java.lang.String response
     * @author zcp
     * @date 2019/1/16 10:34
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/category/edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartCategoryEdit(@RequestBody @Valid ChartCategoryGetDTO chartCategoryGetDTO, BindingResult br) {
        XbbResponse<ChartCategoryGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ChartCategoryGetVO chartCategoryGetVO = chartCategoryService.chartCategoryEdit(chartCategoryGetDTO);
                response = new XbbResponse<>(chartCategoryGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
       return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 分组保存
     * @param chartCategorySaveDTO 入参
     * @param br 校验信息
     * @return java.lang.String response
     * @author zcp
     * @date 2019/1/16 10:37
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/category/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartCategorySave(@RequestBody @Valid ChartCategorySaveDTO chartCategorySaveDTO, BindingResult br) {
        XbbResponse<ChartCategorySaveVO> response;
        //对中文字段name长度为20，英文长度name_en为50的特殊校验
        if (br.hasErrors()){
            try {
                commonHelp.checkNameLength(br.getFieldError());
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
                return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
            }
        }
        try {
            ChartCategorySaveVO chartCategorySaveVO = chartCategoryService.chartCategorySave(chartCategorySaveDTO);
            response = new XbbResponse<>(chartCategorySaveVO);
        } catch (XbbException e) {
            response = new XbbResponse<>(e);
        }
       return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 删除分组----分组内有子分类、有图表的不能删除
     * @param chartCategoryDeleteDTO 入参
     * @param br 校验信息
     * @return java.lang.String response
     * @author zcp
     * @date 2019/1/16 10:48
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/category/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartCategoryDelete(@RequestBody @Valid ChartCategoryDeleteDTO chartCategoryDeleteDTO, BindingResult br) {
        XbbResponse<ChartCategoryDeleteVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ChartCategoryDeleteVO chartCategoryDeleteVO = chartCategoryService.chartCategoryDelete(chartCategoryDeleteDTO);
                response = new XbbResponse<>(chartCategoryDeleteVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
       return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 开启关闭分组
     * @param chartCategoryEnableDTO 入参
     * @param br 校验信息
     * @return java.lang.String response
     * @author zcp
     * @date 2019/1/16 16:33
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/category/enable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartCategoryEnable(@RequestBody @Valid ChartCategoryEnableDTO chartCategoryEnableDTO, BindingResult br) {
        XbbResponse<ChartCategoryEnableVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ChartCategoryEnableVO chartCategoryEnableVO = chartCategoryService.chartCategoryEnable(chartCategoryEnableDTO);
                response = new XbbResponse<>(chartCategoryEnableVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
       return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    /**
     * 分组排序
     * @param chartCategorySortDTO 入参
     * @param br 校验信息
     * @return java.lang.String response
     * @author zcp
     * @date 2019/1/16 22:36
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/category/sort", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartCategorySort(@RequestBody @Valid ChartCategorySortDTO chartCategorySortDTO, BindingResult br) {
        XbbResponse<ChartCategorySortVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ChartCategorySortVO chartCategorySortVO = chartCategoryService.chartCategorySort(chartCategorySortDTO);
                response = new XbbResponse<>(chartCategorySortVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
       return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 移动端分组列表
     * @param chartMobileCategoryListDTO 入参
     * @param br 校验信息
     * @return java.lang.String response
     * @author zcp
     * @date 2019/1/25 21:39
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/mobile/category/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartMobileCategoryList(@RequestBody @Valid ChartMobileCategoryListDTO chartMobileCategoryListDTO, BindingResult br) {
        XbbResponse<List<ChartCategoryListVO>> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                List<ChartCategoryListVO> list = chartCategoryService.chartMobileCategoryList(chartMobileCategoryListDTO);
                response = new XbbResponse<>(list);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 报表分类初始化
     *
     * @param chartCategoryInitDTO 入参
     * @param br                   校验信息
     * @return java.lang.String
     * @author 徐俊杰
     * @date 2019/1/23 15:21
     */
    @RequestMapping(value = "/category/init", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartCategoryInit(@RequestBody @Valid ChartCategoryInitDTO chartCategoryInitDTO, BindingResult br) {
        XbbResponse<ChartCategoryInitVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ChartCategoryInitVO chartCategoryInitVO = chartCategoryService.chartCategoryInit(chartCategoryInitDTO);
                response = new XbbResponse<>(chartCategoryInitVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 分类和图表初始化
     *
     * @param chartCategoryInitDTO 入参（包含corpid）
     * @param br           校验信息
     * @return java.lang.String
     * @author 徐俊杰
     * @date 2019/1/31 16:22
     * @since v1.0
     */
    @RequestMapping(value = "/category/init/categoryAndChart", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String categoryAndChartInit(@RequestBody @Valid ChartCategoryInitDTO chartCategoryInitDTO, BindingResult br) {
        XbbResponse<ChartCategoryInitVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(chartCategoryInitDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(chartCategoryInitDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                ChartCategoryInitVO chartCategoryInitVO = chartCategoryService.categoryAndChartInit(chartCategoryInitDTO);
                response = new XbbResponse<>(chartCategoryInitVO);
            } catch (XbbException e) {
                LOG.warn("初始化首页看板失败", e);
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取首页分类和图表
     *
     * @param chartCategoryInitDTO 入参
     * @throws
     * @return
     * @author hongxiao
     * @date 2021-01-29 16:24
     * @since
     * @version
     */
    @RequestMapping(value = "/get/index/categoryAndChart", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getIndexCategoryAndChart(@RequestBody @Valid ChartCategoryInitDTO chartCategoryInitDTO, BindingResult br) {
        XbbResponse<ChartCategoryInitVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ChartCategoryInitVO chartCategoryInitVO = chartCategoryService.getIndexCategoryAndChart(chartCategoryInitDTO);
                response = new XbbResponse<>(chartCategoryInitVO);
            } catch (XbbException e) {
                LOG.warn("初始化首页看板失败", e);
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 呼叫中心的一级分类及二级分类
     *
     * @param baseDTO 入参（包含corpid）
     * @param br 校验信息
     * @return java.lang.String
     * @author 徐俊杰
     * @date 2019/6/24 17:22
     * @since v1.0
     */
    @RequestMapping(value = "/category/callCenterLink", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String callCenterLink(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<ChartCategoryListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            ChartCategoryListVO chartCategoryListVO = chartCategoryService.callCenterLink(baseDTO);
            response = new XbbResponse<>(chartCategoryListVO);
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 仪表盘公共查询条件新增接口
     * @param categorySearchSaveDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zhouwq
     * @date 2020/8/25 9:26
     */
    @RequestMapping(value = "/dashboard/search/saveBatch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String searchSaveBatch(@RequestBody @Valid CategorySearchSaveDTO categorySearchSaveDTO, BindingResult br) {
        XbbResponse<CategorySearchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // ----------------------------------repeat post----------------------------------------------
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(categorySearchSaveDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(categorySearchSaveDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                CategorySearchVO categorySearchVO = chartCategoryService.categorySearchSaveBatch(categorySearchSaveDTO);
                response = new XbbResponse<>(categorySearchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 仪表盘公共查询条件批量更新接口
     * @param categorySearchSaveBatchDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zhouwq
     * @date 2020/8/25 9:26
     */
    @RequestMapping(value = "/dashboard/search/updateBatch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String searchUpdateBatch(@RequestBody @Valid CategorySearchSaveBatchDTO categorySearchSaveBatchDTO, BindingResult br) {
        XbbResponse<CategorySearchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(categorySearchSaveBatchDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(categorySearchSaveBatchDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                CategorySearchVO categorySearchVO = chartCategoryService.categorySearchUpdateBatch(categorySearchSaveBatchDTO);
                response = new XbbResponse<>(categorySearchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    /**
     * 仪表盘公共查询条件获取 by 分类查询条件ID
     * @param categorySearchGetDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zhouwq
     * @date 2020/8/25 9:26
     */
    @RequestMapping(value = "/dashboard/search/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String searchGet(@RequestBody @Valid CategorySearchGetDTO categorySearchGetDTO, BindingResult br) {
        XbbResponse<CategorySearchGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CategorySearchGetVO categorySearchGetVO = chartCategoryService.categorySearchGet(categorySearchGetDTO);
                response = new XbbResponse<>(categorySearchGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 仪表盘公共查询条件获取列表 by 分类ID
     * @param categorySearchGetListDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zhouwq
     * @date 2020/8/25 9:26
     */
    @RequestMapping(value = "/dashboard/search/getList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String searchGetList(@RequestBody @Valid CategorySearchGetListDTO categorySearchGetListDTO, BindingResult br) {
        XbbResponse<CategorySearchGetListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CategorySearchGetListVO categorySearchGetListVO = chartCategoryService.categorySearchGetList(categorySearchGetListDTO);
                response = new XbbResponse<>(categorySearchGetListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    /**
     * 仪表盘公共文本组件新增接口
     * @param categoryTextSaveDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zhouwq
     * @date 2020/8/25 9:26
     */
    @RequestMapping(value = "/dashboard/text/saveBatch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String textSaveBatch(@RequestBody @Valid CategoryTextSaveDTO categoryTextSaveDTO, BindingResult br) {
        XbbResponse<CategoryTextVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // ----------------------------------repeat post----------------------------------------------
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(categoryTextSaveDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(categoryTextSaveDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                CategoryTextVO categoryTextVO = chartCategoryService.categoryTextSaveBatch(categoryTextSaveDTO);
                response = new XbbResponse<>(categoryTextVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    /**
     * 仪表盘公共文本组件获取 by 分类查询条件ID
     * @param categoryTextGetDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zhouwq
     * @date 2020/8/25 9:26
     */
    @RequestMapping(value = "/dashboard/text/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String textGet(@RequestBody @Valid CategoryTextGetDTO categoryTextGetDTO, BindingResult br) {
        XbbResponse<CategoryTextGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CategoryTextGetVO categoryTextGetVO = chartCategoryService.categoryTextGet(categoryTextGetDTO);
                response = new XbbResponse<>(categoryTextGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 仪表盘公共文本组件获取列表 by 分类ID
     * @param categoryTextGetListDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zhouwq
     * @date 2020/8/25 9:26
     */
    @RequestMapping(value = "/dashboard/text/getList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String textGetList(@RequestBody @Valid CategoryTextGetListDTO categoryTextGetListDTO, BindingResult br) {
        XbbResponse<CategoryTextGetListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CategoryTextGetListVO categoryTextGetListVO = chartCategoryService.categoryTextGetList(categoryTextGetListDTO);
                response = new XbbResponse<>(categoryTextGetListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    /**
     * 仪表盘公共查询字段获取
     * @param categoryFieldGetDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zhouwq
     * @date 2020/8/25 9:26
     */
    @RequestMapping(value = "/dashboard/field/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fieldGet(@RequestBody @Valid CategoryFieldGetDTO categoryFieldGetDTO, BindingResult br) {
        XbbResponse<CategoryFieldGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CategoryFieldGetVO categoryFieldGetVO = chartCategoryService.categoryFieldGet(categoryFieldGetDTO);
                response = new XbbResponse<>(categoryFieldGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 仪表盘权限信息更新接口
     * @param categoryPermissionSaveDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zhouwq
     * @date 2020/8/25 9:26
     */
    @RequestMapping(value = "/dashboard/permission/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String permissionUpdate(@RequestBody @Valid CategoryPermissionSaveDTO categoryPermissionSaveDTO, BindingResult br) {
        XbbResponse<CategorySearchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CategorySearchVO categorySearchVO = chartCategoryService.categoryPermissionUpdate(categoryPermissionSaveDTO);
                response = new XbbResponse<>(categorySearchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 仪表盘发布接口
     * @param categoryPublishDTO 入参
     * @param br 校验参数
     * @return java.lang.String
     * @author zhouwq
     * @date 2020/8/25 9:26
     */
    @RequestMapping(value = "/dashboard/publish", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String categoryPublish(@RequestBody @Valid CategoryPublishDTO categoryPublishDTO, BindingResult br) {
        XbbResponse<CategorySearchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CategorySearchVO categorySearchVO = chartCategoryService.categoryPublish(categoryPublishDTO);
                response = new XbbResponse<>(categorySearchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 旧分类升级为仪表盘
     * @param categoryUpgradeDTO 入参
     * @param br 校验参数
     * @return java.lang.String
     * @author zhouwq
     * @date 2020/8/25 9:26
     */
    @RequestMapping(value = "/dashboard/upgrade", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String categoryUpgrade(@RequestBody @Valid CategoryUpgradeDTO categoryUpgradeDTO, BindingResult br) {
        XbbResponse<CategorySearchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CategorySearchVO categorySearchVO = chartCategoryService.categoryUpgrade(categoryUpgradeDTO);
                response = new XbbResponse<>(categorySearchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 根据分类IdIn获取分类
     * @param chartCategoryListByIdInDTO 入参
     * @param br 校验参数
     * @return java.lang.String
     * @author zhouwq
     * @date 2020/10/23 9:26
     */
    @RequestMapping(value = "/category/listByIdIn", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartCategoryListByIdIn(@RequestBody @Valid ChartCategoryListByIdInDTO chartCategoryListByIdInDTO, BindingResult br) {
        XbbResponse<ChartCategoryListByIdInVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ChartCategoryListByIdInVO result = chartCategoryService.chartCategoryListByIdIn(chartCategoryListByIdInDTO);
                response = new XbbResponse<>(result);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
